﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.Finance;
using Model.Finance.DTO;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;

namespace Reposotory.Finance
{
    public static class AccountBalanceRep
    {
        public static List<AccountBalanceModel> GetMyAccountBalance(this IRepositoryAsync<AccountBalance> repository, int offset, int limit,
            string userId,string BalanceCode)
        {
            string whereCondit = string.Format(@" where  1=1  ");
            if (!string.IsNullOrEmpty(userId))
            {
                whereCondit += string.Format(@" and   AB.UserId = '{0}' ", userId);
            }
            if (!string.IsNullOrEmpty(BalanceCode))
            {
                whereCondit += string.Format(@" and   AB.AccountBalanceCode = '{0}' ", BalanceCode);
            }
            string exeTotalSql = string.Format(@"
             declare   @TOTAL INT            
             select @TOTAL =count(AB.BalanceID) from [WeiXin_BasicInfo].[dbo].[AccountBalance] AB       
                {0}
            ", whereCondit);

            string exeSql = string.Format(@"
      select * from ( 
              SELECT ROW_NUMBER() OVER(order by AB.CreateTime desc) AS RowId,AB.* 
                  ,@TOTAL AS Total
             from [WeiXin_BasicInfo].[dbo].[AccountBalance] AB        
                         
              {0}
            )  as b                      
         where RowId between {1} and {2} 
            ", whereCondit, (offset - 1) * limit, offset * limit);
            return repository.SqlQueryT<AccountBalanceModel>(exeTotalSql + exeSql).ToList();
        }
    }
}
